package com.yunhe.serviceImpl;

import com.yunhe.javabean.Role;
import com.yunhe.mapper.RoleMapper;
import com.yunhe.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    @Override
    public Role findByUid(Integer uid) {
        return roleMapper.selectByUid(uid);
    }

    @Override
    public Role findById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public int edit(Role role) {
        return roleMapper.update(role);
    }

    @Override
    public int add(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public int deleteById(Integer id) {
        return roleMapper.deleteById(id);
    }

    @Override
    public List<Integer> findPermissionByRid(Integer rid) {
        return roleMapper.selectAllPermissionId(rid);
    }

    @Override
    @Transactional
    public void permissionAdd(Integer rid, List<Integer> ids) {
        //先删除关联表中当前角色对应的所有权限
        roleMapper.deletePermissionByRId(rid);
        //使用集合在权限管理表中添加数据
        roleMapper.addPermissionByList(rid,ids);

    }

    @Override
    public Role findRole(Integer id) {
        return roleMapper.selectRole(id);
    }


}
